package jp.seraph.same.view;

import java.util.HashMap;
import java.util.Map;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

import jp.seraph.jsade.model.Align;
import jp.seraph.jspf.pose.DefaultPose;
import jp.seraph.jspf.pose.Pose;
import jp.seraph.same.controller.PoseController;
import jp.seraph.same.model.JointModel;
import jp.seraph.same.model.PoseModel;
import jp.seraph.same.model.PoseModelListener;

/**
 * PosePanel内に配置されるJointの選択ラジオボタンと編集スライダーを管理するパネル
 * 
 */
public class JointPanel extends JPanel {
    public JointPanel(PoseModel aModel, PoseController aController) {
        mMainPanel = new Box(BoxLayout.X_AXIS);
        mSlider = new JointSlider(aModel.getSelectedJointModel(), aController);
        mLabel = new JointValueLabel(aModel.getSelectedJointModel());
        mJointPanelMap = new HashMap<Align, JointListPanel>();
        JointListPanel tLeftPanel = initPanel(new JointListPanel(this, aModel, aController));
        JointListPanel tRightPanel = initPanel(new JointListPanel(this, aModel, aController));
        JointListPanel tCenterPanel = initPanel(new JointListPanel(this, aModel, aController));
        mMainPanel.add(tLeftPanel);
        mMainPanel.add(tCenterPanel);
        mMainPanel.add(tRightPanel);
        mJointPanelMap.put(Align.LEFT, tLeftPanel);
        mJointPanelMap.put(Align.RIGHT, tRightPanel);
        mJointPanelMap.put(Align.CENTER, tCenterPanel);

        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        // this.setLayout(new GridLayout(3, 1));
        this.add(mMainPanel);
        this.add(mSlider);
        this.add(mLabel);

        mListener = new PoseModelListener() {

            public void SelectedJointChanged(PoseModel aSender, int aIndex) {
                JointModel tModel = aSender.getJointModel(aIndex);
                mSlider.setModel(tModel);
                mLabel.setModel(tModel);
            }
        };

        this.setModel(aModel);
    }

    private JointListPanel initPanel(JointListPanel aPanel) {
        aPanel.setLayout(new BoxLayout(aPanel, BoxLayout.Y_AXIS));
        return aPanel;
    }

    private PoseModelListener mListener;
    private PoseModel mModel;
    // private PoseController mController;
    private Box mMainPanel;
    private JointSlider mSlider;
    private JointValueLabel mLabel;
    private Map<Align, JointListPanel> mJointPanelMap;
    private static final long serialVersionUID = 10L;

    public void setModel(PoseModel aModel) {
        if (mModel != null)
            mModel.removeListener(mListener);

        mModel = aModel;
        mModel.addListener(mListener);
        this.refresh();
    }

    public void refresh() {
        PoseModel tSelectedPoseModel = mModel;
        this.clearJointPanel(mJointPanelMap.get(Align.LEFT), tSelectedPoseModel);
        this.clearJointPanel(mJointPanelMap.get(Align.CENTER), tSelectedPoseModel);
        this.clearJointPanel(mJointPanelMap.get(Align.RIGHT), tSelectedPoseModel);

        int tJointCount = tSelectedPoseModel.getJointCount();
        for (int i = 0; i < tJointCount; i++) {
            this.addJointButton(tSelectedPoseModel.getJointModel(i));
        }

        mSlider.setModel(tSelectedPoseModel.getSelectedJointModel());
        mLabel.setModel(tSelectedPoseModel.getSelectedJointModel());

        /*
         * mMainPanel.setSize(this.getWidth(), this.getHeight() -
         * mSlider.getHeight() - mLabel.getHeight());
         * mMainPanel.setVisible(true); mSlider.setSize(this.getWidth(),
         * mSlider.getHeight()); mSlider.setLocation(0, mMainPanel.getHeight());
         * mSlider.setVisible(true); mLabel.setLocation(0,
         * mMainPanel.getHeight() + mSlider.getHeight());
         * mLabel.setVisible(true);
         */
    }

    private void clearJointPanel(JointListPanel aPanel, PoseModel aModel) {
        aPanel.removeAll();
        aPanel.setModel(aModel);
    }

    private void addJointButton(JointModel aModel) {
        mJointPanelMap.get(aModel.getJointAlign()).addJoint(aModel);
    }

    public Pose createPose() {
        DefaultPose tResult = new DefaultPose();
        PoseModel tSelectedPoseModel = mModel;
        int tCount = tSelectedPoseModel.getJointCount();
        for (int i = 0; i < tCount; i++) {
            JointModel tJointModel = tSelectedPoseModel.getJointModel(i);
            tResult.setJointAngle(tJointModel.getJointIdentifier(), tJointModel.getJointAngleAsDegree());
        }

        return tResult;
    }

    public void setRealTimeUpdate(boolean aUpdate) {
        mSlider.setRealTimeUpdate(aUpdate);
    }

    /*
     * public void setModel(PoseModel aModel){ if(aModel == null) throw new
     * NullArgumentException("aModel");
     * 
     * mModel = aModel; this.refresh(); }
     * 
     * public PoseModel getModel(){ return mModel; }
     */
}
